package com.roger.homecenterscreen.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.roger.homecenterscreen.utils.NetworkStatusService

/**
 * 网络状态栏组件
 * 显示WiFi信息和IP地址的自定义标题栏
 */
@Composable
fun NetworkStatusBar(
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val networkService = remember { NetworkStatusService(context) }
    val networkStatus by networkService.networkStatus.collectAsState()
    
    // 组件生命周期管理
    DisposableEffect(Unit) {
        networkService.startMonitoring()
        onDispose {
            networkService.stopMonitoring()
        }
    }
    
    Box(
        modifier = modifier
            .fillMaxWidth()
            .height(32.dp)
            .background(
                color = Color.Black.copy(alpha = 0.3f),
                shape = RoundedCornerShape(6.dp)
            )
            .clickable {
                // 点击刷新网络状态
                networkService.refresh()
            }
            .padding(horizontal = 8.dp, vertical = 4.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 左侧：网络状态图标和类型
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = networkStatus.getStatusIcon(),
                    fontSize = 12.sp,
                    modifier = Modifier.size(16.dp)
                )
                
                Text(
                    text = networkStatus.connectionType,
                    fontSize = 11.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Medium,
                    modifier = Modifier.padding(start = 4.dp)
                )
            }
            
            // 右侧：网络详细信息
            Text(
                text = if (networkStatus.isConnected) {
                    if (networkStatus.connectionType == "WiFi" && networkStatus.ssid != "无WiFi") {
                        "${networkStatus.ssid} | ${networkStatus.ipAddress}"
                    } else {
                        networkStatus.ipAddress
                    }
                } else {
                    // 显示详细的无网络状态信息
                    when {
                        networkStatus.errorMessage != null -> "无网络: ${networkStatus.errorMessage}"
                        networkStatus.connectionType == "网络异常" -> "网络异常"
                        networkStatus.connectionType == "无网络" -> "设备未连接网络"
                        else -> "网络未连接"
                    }
                },
                fontSize = 10.sp,
                color = when {
                    networkStatus.isConnected -> Color.White
                    networkStatus.connectionType == "网络异常" -> Color.Red.copy(alpha = 0.8f)
                    else -> Color.Gray
                },
                fontWeight = FontWeight.Normal,
                maxLines = 1
            )
        }
    }
}

/**
 * 简化版网络状态栏
 * 只显示基本的连接状态和IP地址
 */
@Composable
fun SimpleNetworkStatusBar(
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val networkService = remember { NetworkStatusService(context) }
    val networkStatus by networkService.networkStatus.collectAsState()
    
    DisposableEffect(Unit) {
        networkService.startMonitoring()
        onDispose {
            networkService.stopMonitoring()
        }
    }
    
    Box(
        modifier = modifier
            .fillMaxWidth()
            .height(24.dp)
            .background(
                color = Color.Black.copy(alpha = 0.2f),
                shape = RoundedCornerShape(4.dp)
            )
            .padding(horizontal = 6.dp, vertical = 2.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = networkStatus.getStatusIcon(),
                fontSize = 10.sp,
                modifier = Modifier.padding(end = 4.dp)
            )
            
            Text(
                text = networkStatus.getDisplayText(),
                fontSize = 9.sp,
                color = if (networkStatus.isConnected) Color.White else Color.Gray,
                fontWeight = FontWeight.Normal,
                maxLines = 1
            )
        }
    }
}

/**
 * 紧凑型网络状态栏
 * 适用于空间有限的场景
 */
@Composable
fun CompactNetworkStatusBar(
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val networkService = remember { NetworkStatusService(context) }
    val networkStatus by networkService.networkStatus.collectAsState()
    
    DisposableEffect(Unit) {
        networkService.startMonitoring()
        onDispose {
            networkService.stopMonitoring()
        }
    }
    
    Row(
        modifier = modifier
            .background(
                color = Color.Black.copy(alpha = 0.15f),
                shape = RoundedCornerShape(3.dp)
            )
            .padding(horizontal = 4.dp, vertical = 2.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = networkStatus.getStatusIcon(),
            fontSize = 8.sp,
            modifier = Modifier.padding(end = 2.dp)
        )
        
        Text(
            text = if (networkStatus.isConnected) networkStatus.ipAddress else "无网络",
            fontSize = 8.sp,
            color = if (networkStatus.isConnected) Color.White else Color.Gray,
            maxLines = 1
        )
    }
}